package vamp

import org.springframework.dao.DataIntegrityViolationException

class PoinotationController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [poinotationInstanceList: Poinotation.list(params), poinotationInstanceTotal: Poinotation.count()]
    }

    def create() {
        [poinotationInstance: new Poinotation(params)]
    }

    def save() {
        def poinotationInstance = new Poinotation(params)
        if (!poinotationInstance.save(flush: true)) {
            render(view: "create", model: [poinotationInstance: poinotationInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'poinotation.label', default: 'Poinotation'), poinotationInstance.id])
        redirect(action: "show", id: poinotationInstance.id)
    }

    def show() {
        def poinotationInstance = Poinotation.get(params.id)
        if (!poinotationInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'poinotation.label', default: 'Poinotation'), params.id])
            redirect(action: "list")
            return
        }

        [poinotationInstance: poinotationInstance]
    }

    def edit() {
        def poinotationInstance = Poinotation.get(params.id)
        if (!poinotationInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'poinotation.label', default: 'Poinotation'), params.id])
            redirect(action: "list")
            return
        }

        [poinotationInstance: poinotationInstance]
    }

    def update() {
        def poinotationInstance = Poinotation.get(params.id)
        if (!poinotationInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'poinotation.label', default: 'Poinotation'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (poinotationInstance.version > version) {
                poinotationInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'poinotation.label', default: 'Poinotation')] as Object[],
                        "Another user has updated this Poinotation while you were editing")
                render(view: "edit", model: [poinotationInstance: poinotationInstance])
                return
            }
        }

        poinotationInstance.properties = params

        if (!poinotationInstance.save(flush: true)) {
            render(view: "edit", model: [poinotationInstance: poinotationInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'poinotation.label', default: 'Poinotation'), poinotationInstance.id])
        redirect(action: "show", id: poinotationInstance.id)
    }

    def delete() {
        def poinotationInstance = Poinotation.get(params.id)
        if (!poinotationInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'poinotation.label', default: 'Poinotation'), params.id])
            redirect(action: "list")
            return
        }

        try {
            poinotationInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'poinotation.label', default: 'Poinotation'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'poinotation.label', default: 'Poinotation'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
